1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
|
import { Alert, Platform, View } from "react-native";
import * as Haptics from "expo-haptics";
import { router, Stack, useLocalSearchParams } from "expo-router";
import UpdatingBookmarkList from "@/components/bookmarks/UpdatingBookmarkList";
import FullPageError from "@/components/FullPageError";
import CustomSafeAreaView from "@/components/ui/CustomSafeAreaView";
import FullPageSpinner from "@/components/ui/FullPageSpinner";
import { MenuView } from "@react-native-menu/menu";
import { useMutation, useQuery } from "@tanstack/react-query";
import { Ellipsis } from "lucide-react-native";
import { useTRPC } from "@karakeep/shared-react/trpc";
import { ZBookmarkList } from "@karakeep/shared/types/lists";
export default function ListView() {
const { slug } = useLocalSearchParams();
const api = useTRPC();
if (typeof slug !== "string") {
throw new Error("Unexpected param type");
}
const {
data: list,
error,
refetch,
} = useQuery(api.lists.get.queryOptions({ listId: slug }));
return (
<CustomSafeAreaView>
<Stack.Screen
options={{
headerTitle: list ? `${list.icon} ${list.name}` : "",
headerBackTitle: "Back",
headerLargeTitle: true,
headerRight: () => (
<ListActionsMenu listId={slug} role={list?.userRole ?? "viewer"} />
),
}}
/>
{error ? (
<FullPageError error={error.message} onRetry={() => refetch()} />
) : list ? (
<View>
<UpdatingBookmarkList
query={{
listId: list.id,
}}
/>
</View>
) : (
<FullPageSpinner />
)}
</CustomSafeAreaView>
);
}
function ListActionsMenu({
listId,
role,
}: {
listId: string;
role: ZBookmarkList["userRole"];
}) {
const api = useTRPC();
const { mutate: deleteList } = useMutation(
api.lists.delete.mutationOptions({
onSuccess: () => {
router.replace("/dashboard/lists");
},
}),
);
const { mutate: leaveList } = useMutation(
api.lists.leaveList.mutationOptions({
onSuccess: () => {
router.replace("/dashboard/lists");
},
}),
);
const handleDelete = () => {
Alert.alert("Delete List", "Are you sure you want to delete this list?", [
{ text: "Cancel", style: "cancel" },
{
text: "Delete",
onPress: () => {
deleteList({ listId });
},
style: "destructive",
},
]);
};
const handleLeave = () => {
Alert.alert("Leave List", "Are you sure you want to leave this list?", [
{ text: "Cancel", style: "cancel" },
{
text: "Leave",
onPress: () => {
leaveList({ listId });
},
style: "destructive",
},
]);
};
const handleEdit = () => {
router.push({
pathname: "/dashboard/lists/[slug]/edit",
params: { slug: listId },
});
};
return (
<MenuView
actions={[
{
id: "edit",
title: "Edit List",
attributes: {
hidden: role !== "owner",
},
},
{
id: "delete",
title: "Delete List",
attributes: {
destructive: true,
hidden: role !== "owner",
},
image: Platform.select({
ios: "trash",
}),
},
{
id: "leave",
title: "Leave List",
attributes: {
destructive: true,
hidden: role === "owner",
},
},
]}
onPressAction={({ nativeEvent }) => {
if (nativeEvent.event === "delete") {
handleDelete();
} else if (nativeEvent.event === "leave") {
handleLeave();
} else if (nativeEvent.event === "edit") {
handleEdit();
}
}}
shouldOpenOnLongPress={false}
>
<Ellipsis onPress={() => Haptics.selectionAsync()} color="gray" />
</MenuView>
);
}
|